package test.socket;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import org.apache.commons.codec.binary.Hex;
import org.apache.poi.ss.formula.functions.Hex2Dec;

public class SocketIO {
//	@SuppressWarnings("resource")
	public static void main(String[] args) throws UnknownHostException, IOException, InterruptedException {
		Socket socket = new Socket("127.0.0.1", 32000);
		System.out.println("连接成功。。。");
		Thread.sleep(10000);
		new moreIOThread(socket, "111").start();
		new moreIOThread(socket, "222").start();
	}
	
	static class moreIOThread extends Thread{
		static long bolckStartTime = 0;
		Socket socket;
		String ioName;
		public  moreIOThread(Socket socket,String ioName) {
			this.socket = socket;
			this.ioName = ioName;
		}
		
		@Override
		public void run() {
			try {
				long startTime = System.currentTimeMillis();
				
				InputStream is = socket.getInputStream();
				byte[] bytes = new byte[1024*1000];
				int length = -1;
				while((length = is.read(bytes)) != -1) {
					System.out.println("begin.......");
						
					System.out.println(ioName+":"+length);
					System.out.println(ioName+":"+Hex.encodeHexString(bytes));
					bytes = new byte[1024*1000];
					long time = System.currentTimeMillis() - startTime;
					System.out.println(ioName+"##startTime:"+bolckStartTime);
					System.out.println(ioName+"##useTime:"+(time-bolckStartTime));
					System.out.println(ioName+"##endTime:"+time);
					System.out.println("end........");
					bolckStartTime = time;
					Thread.sleep(10000);
					
				}
				System.err.println("服务端断开连接。。。。");
			} catch (Exception e) {
				System.out.println("error!!");
				e.printStackTrace();
			}
			
			
		}
	}
}
